package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysCommodity;
import com.ruoyi.system.service.ISysCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/commodity")
public class SysCommodityController extends BaseController {

    @Autowired
    private ISysCommodityService iSysCommodityService;

    @RequiresPermissions("system:commodity:list")
    @Log(title = "订单管理", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(SysCommodity sysCommodity) {

        startPage();
        List<SysCommodity> list = iSysCommodityService.selectCommodityList(sysCommodity);
        return getDataTable(list);
    }

    @RequiresPermissions("system:commodity:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCommodity sysCommodity) {

        return toAjax(iSysCommodityService.insertCommodity(sysCommodity));
    }

    @RequiresPermissions("system:commodity:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        iSysCommodityService.deleteCommodityById(id);
        return success();
    }

    @RequiresPermissions("system:commodity:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysCommodity Commodity) {

        return toAjax(iSysCommodityService.updateCommodity(Commodity));
    }

    @RequiresPermissions("system:commodity:query")
    @Log(title = "订单管理", businessType = BusinessType.OTHER)
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {

        return AjaxResult.success(iSysCommodityService.selectCommodityById(id));
    }
}
